//  Inside Microsoft Exchange 2007 Web Services
//  Chapter 7
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.IO;
using ProxyHelpers.EWS;

namespace ForwardIfPossible
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            // Note that you need to get your message from somewhere (not included here)
            //
            MessageType message = null;

            ForwardIfPossible(binding, message);
        }

        /// <summary>
        /// Forward the message if it supports the ForwardItem response object (Listing 7-34)
        /// </summary>
        /// <param name="binding">Binding to use</param>
        /// <param name="message">Message to forward</param>
        /// 
        public static void ForwardIfPossible(ExchangeServiceBinding binding, MessageType message)
        {
            if (message.ResponseObjects == null)
            {
                throw new Exception("Item needs ResponseObjects property");
            }

            // For this example, let's forward the message - so go through the available
            // response objects and see if forward is an option

            if (EWSHelpers.CanPerformResponseObject<ForwardItemType>(message))
            {
                // Let's Forward this email to another recipient
                ForwardItemType forwardItem = new ForwardItemType();
                forwardItem.ReferenceItemId = message.ItemId;
                BodyType newContent = new BodyType();
                newContent.Value = "This is the message I said I'd forward";
                newContent.BodyType1 = message.Body.BodyType1;
                forwardItem.NewBodyContent = newContent;

                EmailAddressType toEmail = new EmailAddressType("jane.dow@contoso.com");
                EmailAddressType[] recipients = new EmailAddressType[] { toEmail };

                forwardItem.ToRecipients = recipients;

                CreateItemType createItem = new CreateItemType();
                createItem.MessageDisposition = MessageDispositionType.SendAndSaveCopy;
                createItem.MessageDispositionSpecified = true;
                createItem.Items = new NonEmptyArrayOfAllItemsType();
                createItem.Items.Items = new ItemType[] { forwardItem };
                CreateItemResponseType createItemResponse = binding.CreateItem(createItem);
                if (createItemResponse.ResponseMessages.Items[0].ResponseCode != ResponseCodeType.NoError)
                {
                    throw new Exception("Failed to forward item.  Response code: " +
                        createItemResponse.ResponseMessages.Items[0].ResponseCode.ToString());
                }
            }
            else
            {
                throw new Exception("Can't forward this item");
            }
        }
    }
}
